import { mock } from 'mockjs';
import menuData from './json/menu.json';

console.log('mock', menuData);

// 定义请求选项的类型
interface RequestOptions {
	body?: string;
	headers?: Record<string, string>;
	[key: string]: any;
}

// 登录
mock('/api/login', 'post', (options: RequestOptions) => {
	const body = JSON.parse(options.body as string);
	const { username, password } = body;
	// 验证
	if (username === 'admin' && password === 'admin123') {
		return {
			code: 200,
			message: 'success',
			data: {
				token: 'your_token',
			},
		};
	} else {
		return {
			code: 401,
			message: '用户名或密码错误',
		};
	}
});

// 获取用户信息
mock('/api/user', 'get', {
	code: 200,
	message: 'success',
	data: {
		'id|1-100': 1,
		name: '@name',
		'age|18-40': 1,
		'gender|1': ['male', 'female'],
	},
});

// 权限
mock('/api/user/permission', 'get', {
	code: 200,
	message: 'success',
	data: menuData,
});
// 用户列表
mock('/api/users', 'get', {
	code: 200,
	message: 'success',
	'data|8': [
		{
			'id|+1': 1,
			avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
			nickName: '@cname',
			'sex|1': [0, 1],
			mobile: /^1[3-9]\d{9}$/,
			createTime: '@datetime("yyyy-MM-dd HH:mm:ss")',
			'status|1': ['0', '1'],
		},
	],
});

// 角色列表
mock('/api/roles', 'get', {
	code: 200,
	message: 'success',
	'data|6': [
		{
			'id|+1': 1,
			nickName: '@cname',
			desc: '@cword(2, 5)',
			remark: '@csentence(4,8)',
			createTime: '@datetime("yyyy-MM-dd HH:mm:ss")',
			'status|1': ['0', '1'],
		},
	],
});

// 部门tree
let deptKey = 1;
mock('/api/dept', 'get', {
	code: 200,
	message: 'success',
	'data|6': [
		{
			title: '@cname(4)',
			key() {
				return deptKey++;
			},
			'children|2': [
				{
					title: '@cname(4)',
					key() {
						return deptKey++;
					},
					children: [],
				},
			],
		},
	],
});

// 字典
mock('/api/dictionary', 'get', {
	code: 200,
	message: 'success',
	'data|6': [
		{
			'id|+1': 1,
			code: '@word(5, 10)',
			name: '@cword(4, 8)',
			'type|1': ['业务级', '菜单级'],
			'structure|0': ['tree', 'peer'],
			'items|3': [{ name: '@cword(3)', value: '@word(3)', desc: '@word(3)' }],
			description: '@cword(5, 9)',
			'status|1': ['0', '1'],
		},
	],
});
